package xcmg.device.web.order;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.order.SubcontractingOrderService;
import xcmg.device.service.vo.order.SubcontractingOrderVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;


/**
 * SubcontractingOrder Controller 实现类
 */
@RestController
@RequestMapping("/subcontractingOrder")
@Api(tags = "转包单")
@Slf4j
public class SubcontractingOrderController extends BasicController {
    @Autowired
    private SubcontractingOrderService subcontractingOrderService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult<PageInfo<SubcontractingOrderVO>> list(@RequestBody SearchCommonVO<SubcontractingOrderVO> condition) throws Exception {
        SubcontractingOrderVO subcontractingOrderVO = condition.getFilters();
        if (subcontractingOrderVO == null) {
            subcontractingOrderVO = new SubcontractingOrderVO();
        }
        if (subcontractingOrderVO.getOrgId() == null) {
            subcontractingOrderVO.setDownOrgId(getOrgId());
        }
        condition.setFilters(subcontractingOrderVO);
        PageCommonVO<SubcontractingOrderVO> pageCommonVO = subcontractingOrderService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/item", method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult<SubcontractingOrderVO> item(String id) {
        return actionValueResult(subcontractingOrderService.getById(id));
    }

    @RequestMapping(value = "/inStock", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true, code = AuthCode.Client.EDIT)
    public ActionResult<String> inStock(@RequestBody SubcontractingOrderVO subcontractingOrderVO) throws Exception {
        if (subcontractingOrderVO.getId() == null) {
            return actionResult(ErrorCode.IllegalArument);
        }
        subcontractingOrderVO.setOrgId(getOrgId());
        subcontractingOrderVO.setUpdateId(getUserId());
        subcontractingOrderVO.setUpdateName(getUserName());
        ErrorCode code = subcontractingOrderService.inStock(subcontractingOrderVO);
        return actionResult(code);
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true, code = AuthCode.Client.DEL)
    public ActionResult<String> delete(@RequestBody SubcontractingOrderVO subcontractingOrderVO) {
        if (subcontractingOrderVO.getId() == null) {
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode code = subcontractingOrderService.delete(subcontractingOrderVO.getId(), getUserId());
        return actionResult(code);
    }
}
